package com.auth.repository;


import com.auth.domain.AuthUser;
import com.marry.contract.provider.UserApi;
import com.marry.contract.vo.MenuVo;
import com.marry.contract.vo.RoleVo;
import com.marry.contract.vo.SysUser;
import com.marry.core.constants.JWTConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @author: 杨超
 * @Date: 2020/8/12 上午9:13
 * @editNote：LiuYun 2021/07/04 22:18:23
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Component
public class MyUserDetailsService implements ReactiveUserDetailsService {

    @Resource
    private UserApi userApi;


    @Override
    public Mono<UserDetails> findByUsername(String username) {

        log.info("开始校验登录参数");

        Mono<SysUser> result = userApi.findByName(username);
        return result.map(x->{
            System.out.println(x.toString());
            Set<SimpleGrantedAuthority> grantedAuthorities = new HashSet<>();
            SysUser sysUser = new SysUser();
            BeanUtils.copyProperties(x, sysUser);

            Flux<RoleVo> roleInfo = userApi.getRoleByUserId(sysUser.getId());
            roleInfo.subscribe(r -> {
                System.out.println(r);
                // 角色必须是ROLE_开头，可以在数据库中设置
                SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(JWTConstants.ROLE_PREFIX + r.getValue());
                grantedAuthorities.add(grantedAuthority);

                // 获取权限
                Flux<MenuVo> menuInfo = userApi.getRolePermission(String.valueOf(r.getId()));
                menuInfo.subscribe(m -> {
                    System.out.println(m);
                    if (!StringUtils.isEmpty(m.getUrl())) {
                        SimpleGrantedAuthority authority = new SimpleGrantedAuthority(m.getUrl());
                        grantedAuthorities.add(authority);
                    }
                });
            });
            AuthUser authUser = new AuthUser(sysUser.getUsername(), sysUser.getPassword(), grantedAuthorities);
            return authUser;
        });

        /*if (result.getCode() != Constants.SUCCESS || result.getResult() == null) {
            throw new BusinessException(400,"用户:" + username + ",不存在!");
        }

        Set<SimpleGrantedAuthority> grantedAuthorities = new HashSet<>();
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(result, userVo);

        Flux<RoleVo> roleInfo = userApi.getRoleByUserId(String.valueOf(userVo.getId()));
        if (roleInfo.getCode() == Constants.SUCCESS) {
            List<RoleVo> roleVoList = roleInfo.getResult();
            for (RoleVo role : roleVoList) {
                // 角色必须是ROLE_开头，可以在数据库中设置
                SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(JWTConstants.ROLE_PREFIX + role.getValue());
                grantedAuthorities.add(grantedAuthority);

                // 获取权限
                Flux<MenuVo> menuInfo = userApi.getRolePermission(String.valueOf(role.getId()));
                if (menuInfo.getCode() == Constants.SUCCESS) {
                    List<MenuVo> permissionList = menuInfo.getResult();
                    for (MenuVo menu : permissionList) {
                        if (!StringUtils.isEmpty(menu.getUrl())) {
                            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(menu.getUrl());
                            grantedAuthorities.add(authority);
                        }
                    }
                }
            }
        }

        AuthUser user = new AuthUser(userVo.getUsername(), userVo.getPassword(), grantedAuthorities);
        user.setId(userVo.getId());

        return Mono.just(user);*/
    }


}
